<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="就诊时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择就诊时间" v-decorator="['treatmentTime']" :trigger-change="true" style="width: 100%"
                  disabled="true"/>
        </a-form-item>
        <a-form-item label="陪诊师" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['escortId',validatorRules.escortId]"
                    placeholder="请选择陪诊师">
            <a-select-option v-for="item in escortSelectList"
                             :value="item.id"
                             :key="item.id">{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: "MedicalServiceOrderModal",
    components: {
      JDate,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          escortId: {
            rules: [{required: true, message: '请选择陪诊师!'},]
          },
        },
        url: {
          add: "/order/medicalServiceOrder/add",
          edit: "/order/medicalServiceOrder/edit",
          getEscortSelectList: "/escort/medicalEscortInfo/getEscortSelectList",
          orderAllocation: "/order/medicalServiceOrder/orderAllocation",
        },
        escortSelectList: []
      }
    },
    created() {},
    methods: {
      getEscortSelectList(orderId) {
        getAction(this.url.getEscortSelectList,{orderId: orderId}).then((res) => {
          if (res.success) {
            this.escortSelectList = res.result;
          }
        });
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.getEscortSelectList(record.id);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'userId', 'parentId', 'serviceId', 'hospitalId', 'orderSn', 'treatmentTime', 'patientId', 'telephone', 'escortId', 'payAmount', 'payType', 'payStatus', 'payTime', 'status', 'serviceStartTime', 'serviceEndTime', 'profitRatio', 'commissionRatio', 'profitAmount', 'commissionAmount', 'allocationStatus', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let param = {
              id: formData.id,
              escortId: formData.escortId,
            }
            httpAction(that.url.orderAllocation, param, 'post').then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'userId', 'parentId', 'serviceId', 'hospitalId', 'orderSn', 'treatmentTime', 'patientId', 'telephone', 'escortId', 'payAmount', 'payType', 'payStatus', 'payTime', 'status', 'serviceStartTime', 'serviceEndTime', 'profitRatio', 'commissionRatio', 'profitAmount', 'commissionAmount', 'allocationStatus', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
      },


    }
  }
</script>